package com.grape.service.base.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.grape.dao.base.AreaRepository;
import com.grape.dao.base.FixedAreaRepository;
import com.grape.dao.base.SubAreaRepository;
import com.grape.domain.base.Area;
import com.grape.domain.base.Courier;
import com.grape.domain.base.FixedArea;
import com.grape.domain.base.SubArea;
import com.grape.service.base.SubAreaService;

@Service
public class SubAreaServiceImpl implements SubAreaService {

	@Autowired
	private SubAreaRepository sar;
	
	@Autowired
	private AreaRepository ar;
	
	@Autowired
	private FixedAreaRepository far;
	
	@Override
	public void saveBatch(HSSFSheet sheetAt) {
		// 保存处理好的每个地区
		List<SubArea> la = new ArrayList<>();
		
		for (Row row : sheetAt) {
			// 跳过表头
			if (row.getRowNum()==0) {
				continue;
			}
			
			
			SubArea subArea = new SubArea();
			// 分区id
			subArea.setId(row.getCell(0).getStringCellValue());
			// 定区
			String dqid = row.getCell(1).getStringCellValue();
			FixedArea dq = far.findOne(dqid);
			subArea.setFixedArea(dq);
			// 区域
			String quid = row.getCell(2).getStringCellValue();
			Area qy = ar.findOne(quid);
			subArea.setArea(qy);
			// 关键字
			subArea.setKeyWords(row.getCell(3).getStringCellValue());
			// 起始号
			subArea.setStartNum(row.getCell(4).getStringCellValue());
			// 结束号
			subArea.setEndNum(row.getCell(5).getStringCellValue());
			// 单双号
			subArea.setSingle(row.getCell(6).getStringCellValue().charAt(0));
			// 位置信息
			subArea.setAssistKeyWords(row.getCell(7).getStringCellValue());
			
			la.add(subArea);
		}
		
		sar.save(la);
	}

	@Override
	public Page<SubArea> findAll(final SubArea model,Pageable pageable) {
		Specification<SubArea> sc = new Specification<SubArea>() {
			@Override
			public Predicate toPredicate(Root<SubArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// 条件集合
				List<Predicate> list = new ArrayList<>();
				
				// 条件一：指定地域
				Area area = model.getArea();
				if (area!=null) {
					
					Join<Object, Object> areaJoin = root.join("area",JoinType.INNER);
					if ( StringUtils.isNotBlank(area.getProvince())) {
						Predicate p1 = cb.like(areaJoin.get("province").as(String.class), "%"+area.getProvince()+"%"); 
						list.add(p1);
					}
					if (StringUtils.isNotBlank(area.getCity())) {
						Predicate p2 = cb.like(areaJoin.get("city").as(String.class), "%"+area.getCity()+"%"); 
						list.add(p2);
					}
					if (StringUtils.isNotBlank(area.getDistrict())) {
						Predicate p3 = cb.like(areaJoin.get("district").as(String.class), "%"+area.getDistrict()+"%"); 
						list.add(p3);
					}
				}
				
				// 条件二：指定定区编码
				FixedArea fixedArea = model.getFixedArea();
				if (fixedArea!=null && StringUtils.isNotBlank(fixedArea.getId())) {
					// 源码中如果fixedArea是null,join是怎么处理的
					Join<Object, Object> fixedAreajoin = root.join("fixedArea",JoinType.INNER);
					Predicate p4 = cb.like(fixedAreajoin.get("id").as(String.class),"%"+fixedArea.getId()+"%");
					list.add(p4);
				}
				
				// 条件三：关键字查询
				if (StringUtils.isNotBlank(model.getKeyWords())) {
					Predicate p5 = cb.like(root.get("keyWords").as(String.class), "%"+model.getKeyWords()+"%");
					list.add(p5);
				}
				
				// 将所有条件融合,数组里只能写0,别想太多								
				return cb.and(list.toArray(new Predicate[0]));
			}
		};	
		return sar.findAll(sc,pageable);
	}

}
